package cn.tedu.lonnghospital.appointment.service.impl;

import cn.tedu.lonnghospital.appointment.ex.ServiceException;
import cn.tedu.lonnghospital.appointment.mapper.AppointmentMapper;
import cn.tedu.lonnghospital.appointment.mapper.DoctorAppointmentTimeMapper;
import cn.tedu.lonnghospital.appointment.mapper.DoctorMapper;
import cn.tedu.lonnghospital.appointment.mapper.PatientMapper;
import cn.tedu.lonnghospital.appointment.pojo.dto.AppointmentAddNewDTO;
import cn.tedu.lonnghospital.appointment.pojo.dto.AppointmentUpdateDTO;
import cn.tedu.lonnghospital.appointment.pojo.entity.Appointment;
import cn.tedu.lonnghospital.appointment.pojo.entity.Doctor;
import cn.tedu.lonnghospital.appointment.pojo.entity.DoctorAppointmentTime;
import cn.tedu.lonnghospital.appointment.pojo.entity.Patient;
import cn.tedu.lonnghospital.appointment.pojo.vo.AppointmentListItemVO;
import cn.tedu.lonnghospital.appointment.pojo.vo.AppointmentStandardVO;
import cn.tedu.lonnghospital.appointment.pojo.vo.PageData;
import cn.tedu.lonnghospital.appointment.service.AppointmentService;
import cn.tedu.lonnghospital.appointment.util.PageInfoToPageDataConverter;
import cn.tedu.lonnghospital.appointment.web.ServiceCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AppointmentServiceImpl implements AppointmentService {
    @Autowired
    AppointmentMapper appointmentMapper;
    @Autowired
    DoctorAppointmentTimeMapper doctorAppointmentTimeMapper;
    @Autowired
    PatientMapper patientMapper;
    @Autowired
    DoctorMapper doctorMapper;
    @Override
    public void addNew(AppointmentAddNewDTO appointmentAddNewDTO) {
        QueryWrapper<DoctorAppointmentTime> qwdat=new QueryWrapper<>();
        qwdat.eq("doctor_id",appointmentAddNewDTO.getDoctorId()).eq("appointment_time_id",appointmentAddNewDTO.getAppointmentTimeId());
        Integer count=doctorAppointmentTimeMapper.selectCount(qwdat);
        if(count<1){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"添加预约失败！该医生该时间段没有号！");
        }
        QueryWrapper<Appointment> qwr=new QueryWrapper<>();
        qwr.eq("appointment_time_id", appointmentAddNewDTO.getAppointmentTimeId()).eq("doctor_id", appointmentAddNewDTO.getDoctorId()).eq("appointment_status","已预约");
        count = appointmentMapper.selectCount(qwr);
        if(count>0){
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"预约失败！该时间段已被预约！");
        }
        Appointment appointment =new Appointment();
        BeanUtils.copyProperties(appointmentAddNewDTO, appointment);
        appointment.setAppointmentStatus("已预约");
        int rows=appointmentMapper.insert(appointment);
        if(rows!=1){
            throw new ServiceException(ServiceCode.ERROR_INSERT,"服务器忙，请稍后再试！");
        }
    }

    @Override
    public void deleteById(long id) {
        Appointment appointment=appointmentMapper.selectById(id);
        if(appointment==null||!appointment.getAppointmentStatus().equals("已预约")){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"取消预约失败！该预约不存在！");
        }
        appointment.setAppointmentStatus("已取消");
        int rows=appointmentMapper.updateById(appointment);
        if(rows!=1){
            throw new ServiceException(ServiceCode.ERROR_DELETE,"服务器忙，请稍后再试！");
        }
        }

    @Override
    public PageData<AppointmentListItemVO> listByPatientId(Long patientId, Integer pageNum, Integer pageSize) {
        QueryWrapper<Patient> qwp=new QueryWrapper<>();
        qwp.eq("id",patientId);
        Integer count=patientMapper.selectCount(qwp);
        if(count<1){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"患者id不存在！");
        }
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<AppointmentListItemVO> pageInfo=new PageInfo<>(appointmentMapper.listByPatientId(patientId));
        PageData<AppointmentListItemVO> pageData= PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<AppointmentListItemVO> listByPatientId(Long patientId, Integer pageNum) {
        return listByPatientId(patientId,pageNum,10);
    }

    @Override
    public AppointmentStandardVO getStandard(long id) {
        QueryWrapper<Appointment> qwa=new QueryWrapper<>();
        qwa.eq("id",id);
        Integer count=appointmentMapper.selectCount(qwa);
        if(count<1){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"预约id不存在!");
        }
        return appointmentMapper.getStandard(id);
    }

    @Override
    public void updateById(long id, AppointmentUpdateDTO appointmentUpdateDTO){
        QueryWrapper<DoctorAppointmentTime> qwdat=new QueryWrapper<>();
        qwdat.eq("doctor_id",appointmentUpdateDTO.getDoctorId()).eq("appointment_time_id",appointmentUpdateDTO.getAppointmentTimeId());
        Integer count=doctorAppointmentTimeMapper.selectCount(qwdat);
        if(count<1){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"修改预约失败！该医生该时间段没有号！");
        }
        QueryWrapper<Appointment> qwr=new QueryWrapper<>();
        qwr.eq("appointment_time_id", appointmentUpdateDTO.getAppointmentTimeId()).eq("doctor_id", appointmentUpdateDTO.getDoctorId())
                .eq("appointment_status","已预约").ne("id",id);
        count = appointmentMapper.selectCount(qwr);
        if(count>0){
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"修改预约失败！该时间段已被预约！");
        }
        Appointment appointment =new Appointment();
        BeanUtils.copyProperties(appointmentUpdateDTO, appointment);
        appointment.setAppointmentStatus("已预约").setId(id);
        int rows=appointmentMapper.updateById(appointment);
        if(rows!=1){
            throw new ServiceException(ServiceCode.ERROR_INSERT,"服务器忙，请稍后再试！");
        }
    }

    @Override
    public PageData<AppointmentListItemVO> listByDoctorId(Long doctorId, Integer pageNum, Integer pageSize) {
        QueryWrapper<Doctor> qwd=new QueryWrapper<>();
        qwd.eq("id",doctorId);
        Integer count=doctorMapper.selectCount(qwd);
        if(count<1){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"医生id不存在！");
        }
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<AppointmentListItemVO> pageInfo=new PageInfo<>(appointmentMapper.listByDoctorId(doctorId));
        PageData<AppointmentListItemVO> pageData= PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<AppointmentListItemVO> listByDoctorId(Long doctorId, Integer pageNum) {
        return listByDoctorId(doctorId,pageNum,10);
    }

    @Override
    public void updateFinishById(long id) {
        QueryWrapper<Appointment> qwa=new QueryWrapper<>();
        qwa.eq("id",id).eq("appointment_status","已预约");
        Integer count=appointmentMapper.selectCount(qwa);
        if(count<1){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"该预约不存在！");
        }
        Appointment appointment=new Appointment();
        appointment.setId(id).setAppointmentStatus("已就诊");
        Integer rows=appointmentMapper.updateById(appointment);
        if(rows!=1){
            throw new ServiceException(ServiceCode.ERROR_UPDATE,"服务器繁忙请稍后再试！");
        }
    }
}
